package com.zxcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxcl.ex.BusinessException;
import com.zxcl.utils.AuthUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxcl.domain.MemberAddr;
import com.zxcl.mapper.MemberAddrMapper;
import com.zxcl.service.MemberAddrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;



@Service
@CacheConfig(cacheNames = "com.zxcl.service.impl.MemberAddrServiceImpl")
public class MemberAddrServiceImpl extends ServiceImpl<MemberAddrMapper, MemberAddr> implements MemberAddrService{

    /**
     * 会员的收获地址
     *
     * @param openId
     * @return
     */
    @Override
    @Cacheable(key = "#openId")
    public List<MemberAddr> getMemberAddrs(String openId) {
        return this.getBaseMapper().selectList(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId,openId)
                .eq(MemberAddr::getStatus, 1)
                .orderByDesc(MemberAddr::getCommonAddr, MemberAddr::getUpdateTime)
        );
    }

    /**
     * @param memberAddr
     * @return
     */
    @Override
    @CacheEvict(key = "#memberAddr.openId")
    public Integer addMemberAddrs(MemberAddr memberAddr) {
        //查询openId用户的默认收货地址的数量
        Long count = getBaseMapper().selectCount(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, memberAddr.getOpenId())
                .eq(MemberAddr::getCommonAddr, 1)
        );
        //如果没有默认收货地址，就把当前你添加的这个收货地址做为默认收货地址
        if (count <= 0L) {
            memberAddr.setCommonAddr(1);
        }
        memberAddr.setStatus(1);
        memberAddr.setCreateTime(new Date());
        memberAddr.setUpdateTime(new Date());
        return getBaseMapper().insert(memberAddr);
    }

    /**
     * 修改会员的默认收货地址
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    @CacheEvict(key = "#openId")
    public int changeDefaultMemberAddr(Long id,String openId) {
        if(ObjectUtils.isEmpty(openId)){
            throw new BusinessException("会员ID无法获取");
        }
        //更新当前会员的所有地址为非默认
        List<MemberAddr> memberAddrs = this.getBaseMapper().selectList(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
        );
        if(!CollectionUtils.isEmpty(memberAddrs)){
            for (MemberAddr memberAddr : memberAddrs) {
                if(memberAddr.getAddrId().equals(id)){
                    memberAddr.setCommonAddr(1);
                }else{
                    memberAddr.setCommonAddr(0);
                }
                this.getBaseMapper().updateById(memberAddr);
            }
            return 1;
        }
        return 0;
    }
}
